package database;

import java.sql.Statement;

import mockup.UserMockup;

import server.database.ConnectionDatabase;

/**
 * Done men mangler javadoc.
 * Skal vi legge til rette for å hente ut passord?
 * @author daniel
 */
public class SettingDB {

	private static String query;
	private Statement st;
	
	public SettingDB() {
		try {
			st = ConnectionDatabase.getConnection().createStatement();			
		} catch (Exception e) {
			System.err.println("AppoitmentDB::Constructor:Exception: " + e.getMessage());
		}
	}

	public void insert(UserMockup user, String password, 
			int color, int sound) {
		if(user.getId() > -1) {
			query = "INSERT INTO " + ConnectionDatabase.getNameDB()+".setting values(" +
			user.getId() + "," + "'" + user.getName() +	"','" + password + "'," +
			color + "," + sound +")";

			ConnectionDatabase.insert(query);

		} else {
			System.err.println("Ingen bruker");
		}
	}

	public void update(UserMockup user, String newPass, 
			int color, int sound, String currentPass) {
		query = "UPDATE " + ConnectionDatabase.getNameDB()+".setting SET "+
		"password = '" + newPass + "'," + 
		"color_theme = " + color + "," +
		"sound_enabled = " + sound +
		" where user_id = " + user.getId() +
		" and password = '" + currentPass + "'";

		ConnectionDatabase.update(query, st);
	}


	// Not tested:
	//
	//	public void get(int id) {
	//		String query = "select user_id, username, " +
	//		"password, color_theme, sound_enabled" + 
	//		ConnectionDatabase.getNameDB() + ".setting";
	//		ResultSet rs = ConnectionDatabase.get(query);
	//
	//		try {
	//			while (rs.next()) {
	//				int settings_id = rs.getInt("user_id");
	//				String username = rs.getString("username");
	//				String password = rs.getString("password");
	//				int colortheme = rs.getInt("color_theme");
	//				int sound_enabled = rs.getInt("sound_enabled");
	//			}
	//		} catch (Exception e) {
	//			// TODO: handle exception
	//		}
	//	}
}